<template>
	<view class="content">
		<view v-if="JSON.stringify(location) !== '{}'">
			经度：{{location.longitude}}，纬度：{{location.latitude}}
		</view>
		<view v-else>
			<button type="primary" size="mini" @click="getLocation">获取地理位置</button>
		</view>
		<uni-popup ref="popupDialog" type="dialog">
			<uni-popup-dialog 
				type="info" 
				title="授权设置提醒" 
				content="检测到您没打开权限，是否去设置打开？"
				before-close="true"
				@confirm="sureOpenSetting"
				@close="cancelOpenSetting"
			>
			</uni-popup-dialog>
		</uni-popup> 
	</view>
</template>

<script>
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue'
	
	export default {
		data() {
			return {
				location: {}
			}
		},
		components: {
			uniPopupDialog
		},
		onLoad() {
		},
		methods: {
			
			async getLocation() {
				const [err, succ] = await uni.getLocation().catch(err => err);
				if (!err && succ && succ.errMsg === 'getLocation:ok') {
					this.location = succ;
				}
				if (err && (err.errMsg === 'getLocation:fail auth deny' || err.errMsg === 'getLocation:fail authorize no response')) {
					this.$refs.popupDialog.open();
				}
			},
			sureOpenSetting(done) {
				// done用来关闭弹窗
				done();
				uni.openSetting({
					success: (res) => {
						if (res.errMsg !== 'openSetting:ok') {
							return;
						}
						if (res.authSetting['scope.userLocation']) {
							uni.$showMsg('授权成功，请再次点击获取地理位置');
						} else {
							uni.$showMsg('您取消了授权');
						}
					}
				})
			},
			cancelOpenSetting(done) {
				// done用来关闭弹窗
				done();
				uni.$showMsg("您取消了授权");
			}
		}
	}
</script>

<style>
	.content {
		padding: 20px 0;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

</style>
